//2009/08/26 20:41:31
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <sstream>
#include <algorithm>

using namespace std;

class FingerCounting
{
public:
    int maxNumber(int weakFinger, int maxCount)
    {
		if(weakFinger == 1 || weakFinger == 5)
		{
			return maxCount*8 + weakFinger - 1;
		}
		else
		{
			if(maxCount % 2 == 0)
			{
				return (maxCount/2) * 8 + weakFinger - 1;
			}
			else
			{
				return (maxCount/2) * 8 + 9 - weakFinger;
			}
		}
    }
};
